package com.yuneec.fly.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by peng.gao on 2016/9/9.
 */
public class RoundCornerRecyclerView extends RecyclerView {
    public RoundCornerRecyclerView(Context context) {
        super(context);
    }

    public RoundCornerRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }


    private static final float OUTLINE_WIDTH = 2;
    private static final float CORNER_RADIUS = 4;
    float[] mOutlineRadius = new float[8];

    {
        for (int i = 0; i < mOutlineRadius.length; i++) {
            mOutlineRadius[i] = CORNER_RADIUS;
        }
    }

    @Override
    public void onDraw(Canvas c) {
        Path outline = new Path();
        outline.addRoundRect(new RectF(0, 0, getWidth(), getHeight()), mOutlineRadius, Path.Direction.CW);
        c.clipPath(outline);
        super.onDraw(c);
    }

    @Override
    public boolean drawChild(Canvas canvas, View child, long drawingTime) {
        super.drawChild(canvas, child, drawingTime);
        Paint mRadioGroupOutlinePaint = new Paint();
        mRadioGroupOutlinePaint.setColor(Color.WHITE);
        mRadioGroupOutlinePaint.setStrokeWidth(OUTLINE_WIDTH);
        mRadioGroupOutlinePaint.setStyle(Paint.Style.STROKE);
        mRadioGroupOutlinePaint.setAntiAlias(true);
        Path outlinePath = new Path();
        outlinePath.addRoundRect(new RectF(OUTLINE_WIDTH / 2, OUTLINE_WIDTH / 2, getWidth() - OUTLINE_WIDTH / 2, getHeight() - OUTLINE_WIDTH / 2), mOutlineRadius, Path.Direction.CW);
        canvas.drawPath(outlinePath, mRadioGroupOutlinePaint);
        return true;
    }
}
